home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / dyn_range / hdp.c next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  2.2 KB  |  98 lines

  1. /*
  2. ** HDP.C : Encoding and Decoding of High Dynamic Range Pixels
  3. */
  4.  
  5. #include <stdio.h>
  6. void *malloc(unsigned int);
  7. #include "hdp.h"
  8.  
  9. /*
  10. ** Encoding and Decoding Look-Up Tables
  11. */
  12. byte *EncodeLut;
  13. real *DecodeLut;
  14. real  LutScale;
  15.  
  16. /*
  17. ** Construction of the Encoding Look-Up Table
  18. **
  19. ** Input :
  20. **    LoVal = Less significant (ie lowest non zero) value of the incoming range
  21. **    HiVal = Most significant (ie highest) value of the incoming range
  22. **    NbVal = Number of elements in the encoding LUT
  23. **
  24. ** Output :
  25. **    The function returns 0 if the allocation failed
  26. */
  27. int init_HDP_encode (real LoVal, real HiVal, int NbVal)
  28. {
  29.     real t, r;
  30.     int     n;
  31.  
  32.   EncodeLut = (byte *) malloc (NbVal * sizeof (byte));
  33.   if (! EncodeLut) return (NULL);
  34.  
  35.   NbVal--;
  36.  
  37. /* Scaling factor = ratio between the encoding LUT and the incoming range */
  38.   LutScale = NbVal / HiVal;
  39.  
  40. /* Bias factor = ratio between the outcoming and the incoming range */
  41.   r = 256.0 * LoVal / HiVal;
  42.  
  43.   for (n = 0; n <= NbVal; n++) {
  44.       t = (float) n / NbVal;
  45.       EncodeLut[n] = 255.0 * t / (t-r*t+r) + 0.5;
  46.   }
  47.   return (! NULL);
  48. }
  49.  
  50. /*
  51. ** Destruction of the Encoding Look-Up Table
  52. */
  53. void exit_HDP_encode (void)
  54. {
  55.   free (EncodeLut);
  56. }
  57.  
  58. /*
  59. ** Construction of the Decoding Look-Up Table
  60. **
  61. ** Input :
  62. **    LoVal  = Less significant (ie lowest non zero) value of the incoming range
  63. **    HiVal  = Most significant (ie highest) value of the incoming range
  64. **    Bright = Brightness factor in the range (-1,1)
  65. **           (Bright < 0 : Image is darkened, Bright > 0 : Image is lightened)
  66. **
  67. ** Output :
  68. **    The function returns 0 if the allocation failed
  69. */
  70. int init_HDP_decode (real LoVal, real HiVal, real Bright)
  71. {
  72.     float t, r;
  73.     int n;
  74.  
  75.   DecodeLut = (real *) malloc (256 * sizeof (real));
  76.   if (! DecodeLut) return (NULL);
  77.  
  78. /* Change Bright from (-1,1) into a scaling coefficient (0,infinity) */
  79.   Bright = Bright < 0.0 ? Bright+1.0 : 1.0 / (1.0-Bright);
  80.  
  81. /* Bias factor = ratio of incoming and outcoming range * brightness factor */
  82.   r = Bright * HiVal / LoVal / 256.0;
  83.  
  84.   for (n = 0; n <= 256; n++) {
  85.     t = (float) n / 255.0;
  86.     DecodeLut[n] = t / (t-t*r+r) * HiVal;
  87.   }
  88.   return (! NULL);
  89. }
  90.  
  91. /*
  92. ** Destruction of the Decoding Look-Up Table
  93. */
  94. void exit_HDP_decode (void)
  95. {
  96.   free (DecodeLut);
  97. }
  98.